package com.iostream;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @Author WaveHi
 * @Date 20/8/2021 下午 2:55
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Test the use of java.oi.FileWriter
 */
public class TestFileWriter {

    public static void main(String[] args) {
        FileReader fr1 = null;
        FileWriter fw1 = null;

        try {
            //获取位置
            File dest = new File("day11/txt/hello2.txt");
            File srcFile = new File("day11/txt/hello.txt");

            //获取流
            fr1 = new FileReader(srcFile);
            fw1 = new FileWriter(dest);

            //存取数据
            int len = 0;
            char[] cbuf = new char[5];
            while((len = fr1.read(cbuf)) != -1) {

                fw1.write(cbuf,0,len);

            }
            System.out.println("Data transfer completed!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(fw1 != null) {

                try {
                    fw1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(fr1 != null) {

                try {
                    fr1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }


    }

}
